<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <!-- <a-input v-model:value.trim="oceanBulkCargoProcessWhere.name" placeholder="请输入" @keyup.enter="oceanBulkCargoProcessReload" @change="oceanBulkCargoProcessReload" allow-clear />-->
              <a-input v-model:value.trim="oceanBulkCargoProcessWhere.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="选择">
              <a-select v-model:value="oceanBulkCargoProcessWhere.sex" placeholder="请选择" allow-clear>
                <a-select-option value="1">1</a-select-option>
                <a-select-option value="2">2</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="oceanBulkCargoProcessReload">查询</a-button>
                <a-button @click="oceanBulkCargoProcessReset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="oceanBulkCargoProcessTableRef" row-key="id" :columns="columns" :datasource="datasource" :scroll="{ x: 1000 }">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="oceanBulkCargoProcessOpenEdit()" v-any-permission="['ocean:oceanBulkCargoProcess:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="oceanBulkCargoProcessOpenEdit(record)" v-any-permission="['ocean:oceanBulkCargoProcess:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="oceanBulkCargoProcessRemove(record)">
                <a class="ele-text-danger" v-any-permission="['ocean:oceanBulkCargoProcess:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 海运散货订单流程状态添加、修改弹窗 -->
    <ele-modal :width="460" v-model:visible="oceanBulkCargoProcessVisible_modal" :confirm-loading="loading" :title="oceanBulkCargoProcessIsUpdate ? '修改海运散货订单流程状态' : '新建海运散货订单流程状态'"
      position="center" :body-style="{ paddingBottom: '8px' }" @ok="oceanBulkCargoProcessSave" :maskClosable="false" cacheKey="oceanBulkCargoProcess">
      <a-form ref="oceanBulkCargoProcessFormRef" :model="oceanBulkCargoProcessForm" :rules="oceanBulkCargoProcessRules" :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-form-item label="订单id" name="bulkCargoId">
          <a-input allow-clear :maxlength="200" placeholder="请输入订单id" v-model:value="oceanBulkCargoProcessForm.bulkCargoId" autocomplete="off" />
        </a-form-item>
        <a-form-item label="状态" name="state">
          <a-input allow-clear :maxlength="200" placeholder="请输入状态" v-model:value="oceanBulkCargoProcessForm.state" autocomplete="off" />
        </a-form-item>
        <a-form-item label="备注" name="remark">
          <a-input allow-clear :maxlength="200" placeholder="请输入备注" v-model:value="oceanBulkCargoProcessForm.remark" autocomplete="off" />
        </a-form-item>
      </a-form>
    </ele-modal>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';
import { TIME_OUT } from '@/config/damon';

import { pageOceanBulkCargoProcess, removeOceanBulkCargoProcess, addOceanBulkCargoProcess, updateOceanBulkCargoProcess } from '@/api/ocean/oceanBulkCargoProcess';

// 表格实例
const oceanBulkCargoProcessTableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (oceanBulkCargoProcessTableRef.value?.tableIndex ?? 0)
  },
  {
    title: '订单id',
    dataIndex: 'bulkCargoId',
    key: 'bulkCargoId',
    // sorter: true
  },
  {
    title: '状态',
    dataIndex: 'state',
    key: 'state',
    // sorter: true
  },
  {
    title: '备注',
    dataIndex: 'remark',
    key: 'remark',
    // sorter: true
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
    hideInSetting: true
  }
]);

// 表单数据
const { form: oceanBulkCargoProcessWhere, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = ({ page, limit, where, orders }) => {
  return pageOceanBulkCargoProcess({ ...where, ...orders, page, limit });
};

/* 搜索 */
const oceanBulkCargoProcessReload = () => {
  oceanBulkCargoProcessTableRef?.value?.reload({ page: 1, where: oceanBulkCargoProcessWhere });
};
const oceanBulkCargoProcessReload2 = () => {
  oceanBulkCargoProcessTableRef?.value?.reload();
};

let oceanBulkCargoProcessWhere_timeout = 0
watch(() => oceanBulkCargoProcessWhere, (val) => {
  clearTimeout(oceanBulkCargoProcessWhere_timeout)
  oceanBulkCargoProcessWhere_timeout = setTimeout(() => {
    oceanBulkCargoProcessReload();
  }, TIME_OUT);
},
{ deep: true }
)

/*  重置 */
const oceanBulkCargoProcessReset = () => {
  resetFields();
  oceanBulkCargoProcessReload();
};

/* 删除单个 */
const oceanBulkCargoProcessRemove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeOceanBulkCargoProcess(record.id).then((msg) => {
    hide();
    message.success(msg);
    oceanBulkCargoProcessReload2();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};

// 当前选中数据
const oceanBulkCargoProcessCurrent = ref(null);

// 弹窗是否打开
const oceanBulkCargoProcessVisible_modal = ref(false);

// 是否是修改
const oceanBulkCargoProcessIsUpdate = ref(false);

// 提交状态
const loading = ref(false);

//
const oceanBulkCargoProcessFormRef = ref(null);

// 表单数据
const { form: oceanBulkCargoProcessForm, resetFields: oceanBulkCargoProcessResetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const oceanBulkCargoProcessRules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
    }
  ],
});

/* 打开编辑弹窗 */
const oceanBulkCargoProcessOpenEdit = (record) => {
  oceanBulkCargoProcessResetFormFields();
  oceanBulkCargoProcessFormRef.value?.clearValidate();
  oceanBulkCargoProcessCurrent.value = record?? {};
  oceanBulkCargoProcessVisible_modal.value = true;
  Object.assign(oceanBulkCargoProcessForm, record ?? {});
  oceanBulkCargoProcessIsUpdate.value = !!record;
};

/* 保存编辑 */
const oceanBulkCargoProcessSave = () => {
  if (!oceanBulkCargoProcessFormRef.value) {
    return;
  }
  oceanBulkCargoProcessFormRef.value.validate().then(() => {
    loading.value = true;
    const saveOrUpdate = oceanBulkCargoProcessIsUpdate.value ? updateOceanBulkCargoProcess : addOceanBulkCargoProcess;
    saveOrUpdate(oceanBulkCargoProcessForm).then((msg) => {
      loading.value = false;
      message.success(msg);
      oceanBulkCargoProcessVisible_modal.value = false;
      oceanBulkCargoProcessReload2();
    }).catch((e) => {
      loading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};
</script>

<script>
export default {
  name: 'OceanBulkCargoProcess'
};
</script>

